# -*- coding:utf-8 -*-

import json
import requests

hlist = []
header = {
    'content-type': "application/json;charset=UTF-8"
}


def TestPostRequest(hurl, hdata, headers, htestcassid, htestcasename, htesthope, fanhuitesthope):
    global hlist
    hr = requests.post(hurl, data=hdata, headers=headers)
    hresult = json.loads(hr.text)
    hstatus = hresult['status']
    if hstatus == htesthope and fanhuitesthope in str(hresult):
        hhhdata = {"t_id": htestcassid,
                   "t_name": htestcasename,
                   "t_method": "POST",
                   "t_url": hurl,
                   "t_param": "测试数据:" + str(hdata),
                   "t_hope": "status:" + htesthope + "期望结果：" + fanhuitesthope,
                   "t_actual": "status:" + hstatus + "实际返回结果：" + str(hresult),
                   "t_result": "通过"
                   }
        hlist.append(hhhdata)
        print(hhhdata)
    else:
        hhhdata = {"t_id": htestcassid,
                   "t_name": htestcasename,
                   "t_method": "POST",
                   "t_url": hurl,
                   "t_param": "测试数据:" + str(hdata),
                   "t_hope": "status:" + htesthope + "期望结果：" + fanhuitesthope,
                   "t_actual": "status:" + hstatus + "实际返回结果：" + str(hresult),
                   "t_result": "失败"
                   }
        hlist.append(hhhdata)
        print(hhhdata)
    # print(hlist)


def TestGetRequest(hurl, hdata, headers, htestcassid, htestcasename, htesthope, fanhuitesthope,st):
    global hlist
    if hdata == "":
        hr = requests.get(hurl, headers=headers)
    else:
        hr = requests.get(hurl, params=hdata)
    hresult = json.loads(hr.text)
    hstatus = str(hresult[st])
    if hstatus == htesthope and fanhuitesthope in str(hresult):
        hhhdata = {"t_id": htestcassid,
                   "t_name": htestcasename,
                   "t_method": "GET",
                   "t_url": hurl,
                   "t_param": "测试数据:" + str(hdata),
                   "t_hope": "status:" + htesthope + "期望结果：" + fanhuitesthope,
                   "t_actual": "status:" + hstatus + "实际返回结果：" + str(hresult),
                   "t_result": "通过"
                   }
        hlist.append(hhhdata)
        print(hhhdata)
    else:
        hhhdata = {"t_id": htestcassid,
                   "t_name": htestcasename,
                   "t_method": "GET",
                   "t_url": hurl,
                   "t_param": "测试数据:" + str(hdata),
                   "t_hope": "status:" + htesthope + "期望结果：" + fanhuitesthope,
                   "t_actual": "status:" + hstatus + "实际返回结果：" + str(hresult),
                   "t_result": "失败"
                   }
        hlist.append(hhhdata)
        print(hhhdata)
    # print(hlist)

#把所有的数据整合放到列表中
def TestPostRequestCookie(hurl, hdata, headers, htestcassid, htestcasename, htesthope, fanhuitesthope,cookies,st):
    global hlist
    hr = requests.post(hurl, data=hdata, headers=headers,cookies=cookies)
    hresult = json.loads(hr.text)
    hstatus = str(hresult[st])
    if hstatus == htesthope and fanhuitesthope in str(hresult):
        hhhdata = {"t_id": htestcassid,
                   "t_name": htestcasename,
                   "t_method": "POST",
                   "t_url": hurl,
                   "t_param": "测试数据:" + str(hdata),
                   "t_hope": "status:" + str(htesthope) + "期望结果：" + str(fanhuitesthope),
                   "t_actual": "status:" + str(hstatus) + "实际返回结果：" + str(hresult),
                   "t_result": "通过"
                   }
        hlist.append(hhhdata)
        print(hhhdata)
    else:
        hhhdata = {"t_id": htestcassid,
                   "t_name": htestcasename,
                   "t_method": "POST",
                   "t_url": hurl,
                   "t_param": "测试数据:" + str(hdata),
                   "t_hope": "status:" + str(htesthope) + "期望结果：" + fanhuitesthope,
                   "t_actual": "status:" + str(hstatus) + "实际返回结果：" + str(hresult),
                   "t_result": "失败"
                   }

        hlist.append(hhhdata)
        print(hhhdata)
